package networkcontroller.xml;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLGet extends XML
{
	/**
	 * class describing get packet data
	 * 
	 * @param sum
	 *            hash of file to get
	 * @param size
	 *            size of file to get
	 * @param segment
	 *            segment to get
	 */
	public XMLGet(String sum, long size, int segment)
	{
		/*
		 * <get> <sum></sum> <size></size> <segment></segment> </get>
		 */
		super();
		createNewDocument();
		Node getNode = document.createElement("get");
		document.appendChild(getNode);
		Node sumNode = document.createElement("sum");
		sumNode.setTextContent(sum);
		getNode.appendChild(sumNode);
		Node sizeNode = document.createElement("size");
		sizeNode.setTextContent(String.valueOf(size));
		getNode.appendChild(sizeNode);
		Node segmentNode = document.createElement("segment");
		segmentNode.setTextContent(String.valueOf(segment));
		getNode.appendChild(segmentNode);
	}

	/**
	 * creates xml get from data
	 * 
	 * @param XMLData
	 *            byte array of data
	 */
	public XMLGet(byte[] XMLData)
	{
		super(XMLData);
	}

	/**
	 * gets hash of file
	 * 
	 * @return hash of file
	 */
	public String getSum()
	{
		NodeList sums = document.getElementsByTagName("sum");
		if (sums.item(0) == null)
			return null;
		return sums.item(0).getTextContent();
	}

	/**
	 * gets size of file
	 * 
	 * @return size of file
	 */
	public int getSize()
	{
		NodeList sizes = document.getElementsByTagName("size");
		if (sizes.item(0) == null)
			return 0;
		return Integer.parseInt(sizes.item(0).getTextContent());
	}

	/**
	 * gets number of semgment
	 * 
	 * @return number of segment
	 */
	public int getSegment()
	{
		NodeList segments = document.getElementsByTagName("segment");
		if (segments.item(0) == null)
			return 0;
		return Integer.parseInt(segments.item(0).getTextContent());
	}
}
